package com.ibm.commerce.url.tasks.orgadminconsole;

/**
 * Info 1
 * Info 2
 * Info 3
 * Info 4
 * Info 5
 * Info 6
 */

import com.ibm.commerce.url.harness.*;
import com.ibm.commerce.url.util.Log;
import com.ibm.commerce.url.exception.*;
import com.ibm.wc.test.exception.DatablockMissingException;

import java.util.LinkedHashMap;
import java.util.Map;

public class AssignRoleToUser extends URLTestTask {
	
	public static final String INPUT_PARAMETER_USERID = "userId";
	public static final String INPUT_PARAMETER_ORGIDLIST = "orgIdList";
	public static final String INPUT_PARAMETER_ROLEIDLIST = "roleIdList";
	
	private static AssignRoleToUser instance = new AssignRoleToUser();
	private AssignRoleToUser(){}
	
	public static void execute() throws TaskFailException, DatablockMissingException {
		instance.runTask();
	}
	public static void execute(int counterIndex) throws TaskFailException, DatablockMissingException {
		instance.runTask(counterIndex);
	}
	
	protected void executeTask() throws TaskFailException {
		assignRoleToUserStep1();
		assignRoleToUserStep2();
	}

	private static Map<String, String> AssignRoleToUser_DIAL001 = new LinkedHashMap<String, String>();
	static{
		AssignRoleToUser_DIAL001.put("memberId","");
		AssignRoleToUser_DIAL001.put("XMLFile","buyerconsole.BuyUserRoles");
	}
	private static Map<String, String> AssignRoleToUser_DIAL002 = new LinkedHashMap<String, String>();
	static{
		AssignRoleToUser_DIAL002.put("XMLFile","buyerconsole.BuyUserRoles");
	}
	private static Map<String, String> AssignRoleToUser_BUYU003 = new LinkedHashMap<String, String>();
	static{
		AssignRoleToUser_BUYU003.put("memberId","");
	}
	private void assignRoleToUserStep1() throws TaskFailException {
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering assignRoleToUserStep1");
			
		putParameterInMap(AssignRoleToUser_DIAL001, "memberId", getParameterFromInputMap(INPUT_PARAMETER_USERID));
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/DialogView", WEB_FORM_GET, AssignRoleToUser_DIAL001);
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/DialogNavigation", WEB_FORM_GET, AssignRoleToUser_DIAL002);
		
		putParameterInMap(AssignRoleToUser_BUYU003, "memberId", getParameterFromInputMap(INPUT_PARAMETER_USERID));	
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/BuyUserRolesView", WEB_FORM_POST, AssignRoleToUser_BUYU003);
		
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting assignRoleToUserStep1");
	}
	
	private static Map<String, String> AssignRoleToUser_MEMB005 = new LinkedHashMap<String, String>();
	static{
		AssignRoleToUser_MEMB005.put("redirecturl","DialogNavigation");
		AssignRoleToUser_MEMB005.put("XML","");
		AssignRoleToUser_MEMB005.put("XMLString","<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
		AssignRoleToUser_MEMB005.put("encoding","UTF-8");
	}
	private static Map<String, String> AssignRoleToUser_NEWD006 = new LinkedHashMap<String, String>();
	static{
		AssignRoleToUser_NEWD006.put("ActionXMLFile","buyerconsole.BuyUserAdminList");
		AssignRoleToUser_NEWD006.put("cmd","BuyAdminConUserAdminView");
	}
	private static Map<String, String> AssignRoleToUser_BUYA008 = new LinkedHashMap<String, String>();
	static{
		AssignRoleToUser_BUYA008.put("startindex","0");
		AssignRoleToUser_BUYA008.put("resultssize","0");
		AssignRoleToUser_BUYA008.put("ActionXMLFile","buyerconsole.BuyUserAdminList");
		AssignRoleToUser_BUYA008.put("storeDir","no");
		AssignRoleToUser_BUYA008.put("encoding","UTF-8");
		AssignRoleToUser_BUYA008.put("listsize","12");
	}
	private void assignRoleToUserStep2() throws TaskFailException{
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering assignRoleToUserStep2");
		
		// Add all the roles of organizations provided in the input.
		StringBuffer portionXML = new StringBuffer();
		String[] orgIds = getParamElements(getParameterFromDataFile(INPUT_PARAMETER_ORGIDLIST));
		String[] roleIds = getParamElements(getParameterFromDataFile(INPUT_PARAMETER_ROLEIDLIST));
		for(int i=0;i<orgIds.length;i++){
			portionXML.append(
					"<roleId" + (i+1) + ">" + roleIds[i] + "</roleId" + (i+1) + ">\r\n" +
					"<orgEntityId" + (i+1) + ">" + orgIds[i] + "</orgEntityId" + (i+1) + ">\r\n"
					);
		}
		
		String memb005_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<XML>" +
            "<memberId>" + getParameterFromInputMap(INPUT_PARAMETER_USERID) + "</memberId>" +
            "<XMLFile>buyerconsole.BuyUserRoles</XMLFile>" +
            portionXML +                                    
            "<redirecturl>DialogNavigation</redirecturl>" +
            "<allgrpList></allgrpList>" +
            "<originalList></originalList>" +
            "<assignedList></assignedList>" +
            "<accessgroup></accessgroup>" +
            "</XML>";
		putParameterInMap(AssignRoleToUser_MEMB005, "XML", memb005_XML);		
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/MemberRoleUpdate", WEB_FORM_POST, AssignRoleToUser_MEMB005);
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/NewDynamicListView", WEB_FORM_GET, AssignRoleToUser_NEWD006);
		WebHit("https://standardhost/webapp/wcs/orgadmin/servlet/BuyAdminConUserAdminView", WEB_FORM_POST, AssignRoleToUser_BUYA008);
		GUIValidate("Logon ID");
		
				
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting assignRoleToUserStep2");	
	}	
}
